<?php
/**
*  Create On 2014-12-05
*  Author yiwei
*  QQ:1006629314
**/
class homework extends Model{
	private $table="xdf_homework";
	var $_homework_image_table="xdf_homework_feedback";
	var $_image_table="xdf_image";
	private $fields=array("");
	
	//获取学生反馈的作业图片列表
	public function getFeedbackImageList($homework_id,$student_id){
		$sql = "SELECT i.id as image_id,i.name 
		FROM `xdf_homework_feedback` hf 
		RIGHT JOIN `xdf_homework_image` hi ON hi.homework_feedback_id = hf.id
		LEFT JOIN `xdf_image` i ON  i.id=hi.image_id 
		WHERE hf.homework_id = $homework_id AND hf.student_id = $student_id ";
		return $this->fetchAll($sql);
	}
	public function getHomeworkById($id){
		$sql = 'SELECT * FROM `'.$this->table.'` WHERE `id` = '.$id;
		return $this->fetchRow($sql);
	}
	
	//推送时取出未来几分钟内要推送的作业信息
	public function getHomeworkForPush($start,$minute){
		$starttime = strtotime($start);
		$endtime = strtotime('+'.$minute.'minute',$starttime);
		$sql = 'SELECT HP.`id`,HP.`period_id`,HP.`homework_id` FROM `xdf_homework` AS H
				LEFT JOIN `xdf_homework_period` AS HP ON H.id = HP.homework_id
				WHERE `publish_type` = 1
				AND UNIX_TIMESTAMP(`publish_time`) >= "'.$starttime.'"
				AND UNIX_TIMESTAMP(`publish_time`) <= "'.$endtime.'"';
		return $this->fetchAll($sql);
	}
	
	public function addHomework($user_id,$statue,$item_num,$item_id_str,$need_feedback,$name = null){
		$sql = 'INSERT INTO `'.$this->table.'` (`teacher_id`,`statue`,`name`,`item_num`,`item_id_str`,`need_feedback`,`create_time`)
				VALUES ("'.$user_id.'",'.$statue.',"'.$name.'","'.$item_num.'","'.$item_id_str.'",'.$need_feedback.',NOW())';
		return $this->insert($sql);
	}
	
	public function modifyHomework($id,$statue,$item_num,$item_id_str,$need_feedback,$name = null){
		$sql = 'UPDATE `'.$this->table.'` SET `statue` = '.$statue.',`name` = "'.$name.'",`item_num` = "'.$item_num.'",
				`item_id_str` = "'.$item_id_str.'",`need_feedback` = "'.$need_feedback.'"
				WHERE `id` = '.$id;
		return $this->update($sql);
	}
	//获取学生的作业题目总数
	public function getStudentItemTotal($student_id){
		$sql="SELECT SUM(item_num) AS `num` FROM `xdf_homework` WHERE `id` IN(
				SELECT `homework_id` FROM `xdf_homework_period` 
				WHERE `publish_type`=3 AND `period_id` IN (
					SELECT `id` AS period_id FROM `xdf_period` WHERE `class_id` IN(
						SELECT `class_id` FROM `xdf_class_student` WHERE `student_id`= '$student_id' 
					)
				)
			);";
		$rows=$this->fetchRow($sql);
 		return $rows["num"];	
	}
	
	//获取教师的作业草稿列表
	public function getHomeworkDraft($user_id){
		$sql = 'SELECT * FROM `xdf_homework` WHERE `teacher_id` = '.$user_id.'
				AND `statue` = 2 ORDER BY `create_time` DESC';
		return $this->fetchAll($sql);
	}
	
	public function delHomework($id){
		$sql = 'DELETE FROM `xdf_homework` WHERE `id` = '.$id;
		return $this->del($sql);
	}
	public function getHomeworkByPeriod($period_id){
		$sql = "SELECT `id` as `homework_id`,`need_feedback` as `need_feedback` FROM `xdf_homework`
			WHERE `id` IN (SELECT `homework_id` FROM `xdf_homework_period` 
							WHERE period_id='$period_id' AND publish_type='3');";
// 		print_r($sql);
// 		echo "<br>";	
		return $this->fetchRow($sql);
	}
}
	
